package cn.uce.cc.srv.utils;

import cn.uce.common.utils.HttpUtils;
import com.alibaba.fastjson.JSONObject;

/**
 * Created by zhaocan on 2018/4/27.
 */
public class CallCenterApiUtil {
	
	private static final String WEBSERVICE = "/webservice/";
	private static final String CLIENT_ID = "client_id";
	private static final String OPEN_ID = "openId";
    /**  获取AccessToken
     *    {
     *      CLIENT_ID:"sandi",
     *      "client_secret":"sandi"，
     *    }
     * */
    public static String getAccessToken(String venusRestUrl,String clientId ,String clientSecret){
        String result = HttpUtils.getJson(venusRestUrl
                + "/webservice/sd-saas/oauth20/getToken/"
                + clientId + "/" + clientSecret);
        return result;
    }

    /**  获取坐席信息
     *    {
     *      OPEN_ID:"33A5A27DFD0E9641E0530100007F8A3E",
     *      CLIENT_ID:"sandi"
     *    }
     * */
    public static String getuserinfo(String venusRestUrl,String accessToken, String clientId,String openId){
        String url = venusRestUrl+WEBSERVICE
                + "sd-saas/user/getuserinfo?accessToken=" + accessToken;

        // 设置参数
        JSONObject jsonParams =new JSONObject();
        jsonParams.put(CLIENT_ID, clientId);
        jsonParams.put(OPEN_ID, openId);

        return HttpUtils.postJson(url, jsonParams);
    }

    /**  获取坐席信息
     *    {
     *      OPEN_ID:"33A5A27DFD0E9641E0530100007F8A3E",
     *      CLIENT_ID:"sandi"，
     *      "userId":"91"
     *    }
     * */
    public static String getAgentByUserId(String venusRestUrl,String accessToken, String clientId,String openId,String userId){
        String url = venusRestUrl+WEBSERVICE
                + "sd-saas/agent/getAgentByUserId?accessToken=" + accessToken;

        // 设置参数
        JSONObject jsonParams =new JSONObject();
        jsonParams.put(CLIENT_ID, clientId);
        jsonParams.put(OPEN_ID, openId);
        jsonParams.put("userId", userId);
        return HttpUtils.postJson(url, jsonParams);
    }
    /**
     * 查询通话记录
     * *   {
     *      OPEN_ID:"33A5A27DFD0E9641E0530100007F8A3E",
     *      CLIENT_ID:"sandi",
     *      "userCode":"8673@sandi.com.cn",
     *      "organizerId":"1",
     *      "offHkTmSt":"2018-04-26 00:00:00",
     *      "offHkTmEd":"2018-04-26 23:59:59"
     *    }
     */
    public static String getAgentCallHistoryList(String venusRestUrl,String accessToken, String clientId,
                                                 String openId, String userCode, String organizerId, String offHkTmSt, String offHkTmEd) {
        // 接口地址
        String url = venusRestUrl+"/webservice/sd-saas/"
                + "call/getAgentCallHistoryList?accessToken=" + accessToken;

        // 设置参数
        JSONObject jsonParams =new JSONObject();
        jsonParams.put(CLIENT_ID, clientId);
        jsonParams.put(OPEN_ID, openId);
        if (userCode != null) {
            jsonParams.put("userCode", userCode);
        }
        if (organizerId != null) {
            jsonParams.put("organizerId", organizerId);
        }
        if (offHkTmSt != null) {
            jsonParams.put("offHkTmSt", offHkTmSt);
        }
        if (offHkTmEd != null) {
            jsonParams.put("offHkTmEd", offHkTmEd);
        }
        // 调用接口
        return HttpUtils.postJson(url, jsonParams);
    }


    /**
     * 查询录音
     * *   {
     *      OPEN_ID:"33A5A27DFD0E9641E0530100007F8A3E",
     *      CLIENT_ID:"sandi",
     *      "agtId":"8673",
     *      "endTime":"2018-04-26 23:59:59",
     *      "startTime":"2018-04-26 00:00:00",
     *      "organizerId":"1"
     *    }
     */
    public static String getRecordList(String venusRestUrl,String accessToken, String clientId,
                                       String openId, String agtId, String startTime, String endTime, String organizerId) {
        // 接口地址
        String url = venusRestUrl+WEBSERVICE
                + "sd-saas/record/getRecordList?accessToken=" + accessToken;

        // 设置参数
        JSONObject jsonParams =new JSONObject();
        jsonParams.put(CLIENT_ID, clientId);
        jsonParams.put(OPEN_ID, openId);
        if (agtId != null) {
            jsonParams.put("agtId", agtId);
        }
        if (endTime != null) {
            jsonParams.put("endTime", endTime);
        }
        if (startTime != null) {
            jsonParams.put("startTime", startTime);
        }
        if (organizerId != null) {
            jsonParams.put("organizerId", Long.parseLong(organizerId));
        }

        // 调用接口
        return HttpUtils.postJson(url, jsonParams);
    }

    /**
     * 下载录音
     */
    public static byte[] downloadRecord(String venusRestUrl,String accessToken, String clientId,
                                        String openId, String callId, String subId, String organizerId) {
        // 接口地址
        String url = String.format(venusRestUrl+"/webservice/sd-saas/"
                        + "record/downloadRecord/%s/%s/%s/%s/%s?accessToken=%s",
                clientId, openId, callId, subId, organizerId, accessToken);

        // 调用接口
        return HttpUtils.getBytes(url);
    }

    public static void main(String[] args) {
//        String userPwd = null;
//        String userCode = null;
//        String userId =null;
//        //获取accessToken
//        String accessTokenStr = CallCenterApiUtil.getAccessToken(CallCenterClient.client_id, CallCenterClient.client_secret);
//        JSONObject jsonAccessToken = JSONObject.parseObject(accessTokenStr);
//        String errcode=(String) jsonAccessToken.get("errcode");
//        if(errcode.equals("0"))
//        {
//            String accessToken = (String) jsonAccessToken.get("accessToken");
//            //获取userid/userpwd
//            String openId = CallCenterClient.openId;//此处需改为动态获取，根据登陆用户。
//            String userInfoStr = CallCenterApiUtil.getuserinfo(accessToken, CallCenterClient.client_id,openId);
//            JSONObject jsonUserInfo = JSONObject.parseObject(userInfoStr);
//            System.err.println(jsonUserInfo);
//            errcode=(String) jsonUserInfo.get("errcode");
//            if(errcode.equals("0"))
//            {
//                JSONObject userInfo = JSONObject.parseObject( jsonUserInfo.getString("value"));
//                userPwd = userInfo.getString("passWord");
//                userId = userInfo.getString("userId");
//                userCode = userInfo.getString("userCode");
//                String agentStr =getAgentByUserId(accessToken, CallCenterClient.client_id, CallCenterClient.openId,userId);
//                JSONObject jsonAgent = JSONObject.parseObject(agentStr);
//                System.err.println(jsonAgent);
//                errcode=(String) jsonAgent.get("errcode");
//                if(errcode.equals("0")) {
//                    JSONObject agent = JSONObject.parseObject(jsonAgent.getString("value"));
//                    userPwd = agent.getString("password");
//                    System.err.print(userPwd);
//                }
//            }
//        }
    }
}
